﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack;
using ServiceStack.OrmLite;
using EquipmentManager.Dtos;
using CryptSharp;
using EquipmentManager.Properties;
using EquipmentManager.OALogin;

namespace EquipmentManager.Services
{
    class LoginService : Service
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public UserLoginResponse Post(UserLogin request)
        {
            List<User> users = Db.Select<User>(q => q.Name == request.UserName);
            if(users.Count != 1)
            {
                if(Settings.Default.UseOALogin)  //尝试使用OA用户登录
                {
                    OaUser user = OaLoginHelper.login(request.UserName, request.PassWord);
                    if(user != null && !string.IsNullOrWhiteSpace(user.user_name))
                    {
                        string sessionid = RequestLoginValidateTools.Login(request.UserName, request.MinutesOfTimeout);
                        this.Response.SetSessionCookie(Settings.Default.SessionCookieName, sessionid);
                        return new UserLoginResponse { SessionID = sessionid };
                    }
                    
                }
                throw HttpError.Conflict("用户名错误，或不存在此用户。");
            }
            else
            {
                if (Crypter.CheckPassword(request.PassWord, users[0].PassWord))
                {
                    //if (request.MinutesOfTimeout == 0) 暂时不允许为0
                    //    request.MinutesOfTimeout = 20;
                    string sessionid = RequestLoginValidateTools.Login(request.UserName, request.MinutesOfTimeout);
                    this.Response.SetSessionCookie(Settings.Default.SessionCookieName, sessionid);
                    return new UserLoginResponse { SessionID = sessionid };
                }
                else
                {
                    throw HttpError.Unauthorized("密码错误");
                }
            }
        }
        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public UserLogoutResponse Delete(UserLogout request)
        {
            RequestLoginValidateTools.Logout(this.Request);
            this.Response.DeleteCookie(Settings.Default.SessionCookieName);
            return new UserLogoutResponse();
        }

        public GetUsersResponse Get(GetUsers request)
        {
            List<User> ress;
            if (request.Id != 0)
            {
                ress = new List<User>();
                ress.Add(Db.SingleById<User>(request.Id));
            }
            else
            {
                ress = Db.Select<User>(request.Filter);
            }

            return new GetUsersResponse { Entities = ress };
        }

        public PostUserResponse Post(PostUser request)
        {
            Db.Save<User>(request.Entity);
            return new PostUserResponse { Id = request.Entity.Id };
        }

        public DeleteUserResponse Delete(DeleteUser request)
        {
            Db.DeleteById<User>(request.Id);
            return new DeleteUserResponse();
        }
    }
}
